package com.imay.live.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.live.R;
import com.imay.live.fragment.RankingFragment;
import com.imay.live.fragment.dynamics.LinearFragment;
import com.imay.live.model.FeedModel;
import com.imay.live.model.RankingModel;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * Created by chan on 2017/7/27 0027.
 */

public class DiscoveryRankingGameListActivity extends BaseRequestActivity {

    private static final String RANKTYPE = "ranktype";
    private static final String RANKTITLE = "ranktitle";
    public static final String MODEL = "model";

    public static final String[] rankTypeArray = {"FruitScore", "FruitSend", "DrawHost", "DrawPlayer"};

    String discovetyGameCommand;

    int count = 100;
    String rankInTime = "week";
    int start;

    ImageView progress;
    View load_layout;
    TextView load_text, load_text2;
    private Animation mRotateAnimation;
    private String rankType;

    public static void start(Context context, String rankType, String rankTitle) {
        Intent starter = new Intent(context, DiscoveryRankingGameListActivity.class);
        starter.putExtra(RANKTYPE, rankType);
        starter.putExtra(RANKTITLE, rankTitle);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dynamicactivity_layout;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ViewUtil.setForgroundRadius($(R.id.fragment_id));
        rankType = getIntent().getStringExtra("ranktype");
        String rankTitle = getIntent().getStringExtra("ranktitle");
        setTitle(rankTitle);

        initLoadLayout();
        if (!TextUtils.isEmpty(rankType)) {
            discovetyGameCommand = ProtoRequestHelper.task(ProtoHttpFactory.getDiscoverRanking(rankType, rankInTime, count, start)).setTag(this.hashCode()).request(this);
        } else {
            loadError(getResources().getString(R.string.str_loaderror));
        }
    }

    void initLoadLayout() {
        progress = $(R.id.progress_icon);
        load_layout = $(R.id.load_layout);
        load_text = $(R.id.load_text);
        load_text.setVisibility(View.GONE);
        load_text2 = $(R.id.load_text2);
        load_text2.setVisibility(View.GONE);
        mRotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.scan_rotate);
        progress.setAnimation(mRotateAnimation);
        load_layout.setVisibility(View.VISIBLE);
        progress.startAnimation(mRotateAnimation);
    }


    void completeLoad() {
        load_layout.setVisibility(View.GONE);
        progress.clearAnimation();
    }

    void loadError(String error) {
        progress.clearAnimation();
        progress.setImageResource(R.mipmap.wushuju);
        load_text.setText(error);
        load_text.setVisibility(View.VISIBLE);
    }

    void loadNetError() {
        progress.clearAnimation();
        progress.setImageResource(R.mipmap.wuwangluokeyong);
        load_text.setText(R.string.error_10001);
        load_text2.setText(R.string.str_check_net);
        load_text.setVisibility(View.VISIBLE);
        load_text2.setVisibility(View.VISIBLE);
    }

    private void setRankModelTag(List<RankingModel> rankingModelList, String type) {
        if (rankingModelList.size() > 0) {
            rankingModelList.get(0).setRankType(type);
        }
    }

    @Override
    public void onRequestSuccess(String command, String data) {

        try {
            if (command.equals(discovetyGameCommand)) {
                Result<ArrayList<RankingModel>> result = JSON.parseObject(data, new TypeReference<Result<List<RankingModel>>>() {
                }.getType());
                if (result.result == CodeSuccess) {
                    if (result.data != null) {
                        completeLoad();
                        getIntent().putParcelableArrayListExtra(MODEL, result.data);
                        setRankModelTag(result.data, rankType);
                        RankingFragment rankingFragment = RankingFragment.newInstance(RankingFragment.TYPE_GAME_RANKING);
                        addFragment(rankingFragment);
                    }
                } else {
                    loadError(ErrorResult.getString(getApplicationContext(), result.result));
                }
            }
        } catch (Exception e) {
            loadError(getResources().getString(R.string.str_loaderror));
        }
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        if (code == 10001) {
            loadNetError();
        } else {
            loadError(getResources().getString(R.string.str_loaderror));
        }
    }

    private void addFragment(Fragment loadFragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment fragment = fragmentManager.findFragmentByTag(RankingFragment.class.toString());
        if (fragment == null) {
            transaction.add(R.id.fragment_id, loadFragment, RankingFragment.class.toString());
            transaction.show(loadFragment);
        } else {
            transaction.show(fragment);
        }
        transaction.commitAllowingStateLoss();
    }
}
